package jlts.front.core;

import jlts.entity.Request;
import jlts.entity.Response;
import jlts.front.ui.ExamFrame;
import jlts.front.ui.LoginFrame;
import jlts.front.ui.MenuFrame;
import jlts.entity.User;

/**
 * @ClassName UiController
 * @Description 功能类
 * 1.切换页面
 * 2.登录逻辑实现
 * @Author Zxeao
 * @Date 2021/12/21 下午1:41
 * @Version V1.0
 */
public class UiController {

    private LoginFrame loginFrame;
    private MenuFrame menuFrame;
    private ExamFrame examFrame;
    /**连接服务器的网线*/
    private Client client;

    public void setClient(Client client) {
        this.client = client;
    }

    public void setLoginFrame(LoginFrame loginFrame) {
        this.loginFrame = loginFrame;
    }

    public void setMenuFrame(MenuFrame menuFrame) {
        this.menuFrame = menuFrame;
    }

    public void setExamFrame(ExamFrame examFrame) {
        this.examFrame = examFrame;
    }

    public void login(){
        String uname= loginFrame.getUnameText();
        String upwd= loginFrame.getUpwdText();
        //连接服务器
        Request request = new Request();
        request.setMethod("login");
        request.setParamTypes(new Class[]{String.class,String.class});
        request.setValues(new Object[]{uname,upwd});
        Response res = client.send(request);
        //根据服务器结果进行判断
        User u= (User) res.getValue();
        if(u==null){
            loginFrame.updateErrMsg("用户名或密码错误");
        }else{
            menuFrame.updateWelcome(u.getRealName());
            loginFrame.setVisible(false);
            menuFrame.setVisible(true);
        }
    }
}
